﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FarmacieDAL;
using System.ComponentModel;
using Telerik.Windows.Controls;
using System.Data.Objects;
using FarmacieDAL.Data;

namespace Farmacie.Controls
{
    /// <summary>
    /// Interaction logic for ComenziControl.xaml
    /// </summary>
    public partial class ComenziControl : UserControl
    {
        private List<Medicament> toateMedicamentele = new List<Medicament>();
        private List<ComandaWrapper> ordersList = new List<ComandaWrapper>();
        private CollectionViewSource orders;

        public ICollectionView ToateComenzile
        {
            get
            {
                return orders.View;
            }
        }

        public ComenziControl()
        {
            InitializeComponent();
            InitializeDataSource();

            this.DataContext = this;
        }

        public void RefreshControl() { }

        private void InitializeDataSource()
        {
            toateMedicamentele.Clear();
            toateMedicamentele.AddRange(MedicamenteManager.ToateMedicamentele);

            foreach (Medicament medicament in toateMedicamentele)
            {
                if (medicament != null)
                {
                    ComandaWrapper order = new ComandaWrapper();
                    order.Cantitate = medicament.CantitateRecomandata;
                    order.Medicament = medicament;
                    ordersList.Add(order);
                }
            }

            orders = new CollectionViewSource();
            orders.Source = ordersList;
        }

        #region Grid Events

        protected void buttonOrder_Click(object sender, EventArgs e)
        {
            OnOrder(sender as RadButton);
        }

        protected void OnOrder(RadButton selectedButton)
        {
            if (selectedButton != null)
            {
                ComandaWrapper selectedOrder = selectedButton.DataContext as ComandaWrapper;

                if (selectedOrder != null)
                {
                    PlaceOrder(selectedOrder);
                    FarmacieDALManager.EntitiesContext.SaveChanges();
                }
            }
        }

        protected void buttonOrderAll_Click(object sender, EventArgs e)
        {
            OnOrderAll();
        }

        protected void OnOrderAll()
        {
            foreach (ComandaWrapper order in ordersList)
            {
                if (order != null)
                    PlaceOrder(order);
            }

            FarmacieDALManager.EntitiesContext.SaveChanges();
        }

        #endregion

        #region Helper

        private void PlaceOrder(ComandaWrapper currentOrder)
        {
            Comanda comanda = new Comanda();

            comanda.Data = DateTime.Now;
            comanda.Cantitate = currentOrder.Cantitate;
            comanda.Cost = currentOrder.Medicament.CostCumparare * currentOrder.Cantitate;
            comanda.Medicament = currentOrder.Medicament;

            Stoc newStoc = new Stoc();
            newStoc.Medicament = currentOrder.Medicament;
            newStoc.Cantitate_Initiala = currentOrder.Cantitate;
            newStoc.Cantitate_Actuala = currentOrder.Cantitate;
            newStoc.Data_Intrare = currentOrder.Data.AddDays(currentOrder.Medicament.Producator.Timp_Livrare);
            newStoc.Data_Expirare = newStoc.Data_Intrare.AddMonths(6);

            FarmacieDALManager.EntitiesContext.AddToComanda(comanda);
            FarmacieDALManager.EntitiesContext.AddToStoc(newStoc);
        }

        #endregion
    }
}
